package com.tsd.jxc.goods.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.goods.dao.JxcCategoryMapper;
import com.tsd.jxc.goods.entity.JxcCategory;
import com.tsd.jxc.goods.entity.JxcCategoryExt;
import com.tsd.jxc.goods.service.JxcCategoryService;
import com.tsd.jxc.utils.TreeUtil;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.tsd.core.constants.Constants.ROOT_PARENT_SID;

/**
 * 描述：JxcCategory 服务实现层
 *
 * @author Hillpool
 * @date 2022/07/29 15:02:35
 */
@Service
public class JxcCategoryServiceImpl extends BaseServiceImpl implements JxcCategoryService {

    @Resource
    private JxcCategoryMapper jxcCategoryMapper;

    @Override
    public JxcCategory getJxcCategoryById(Long id) throws Exception {
        return jxcCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcCategoryExt queryBySid(String sid) throws Exception {
        return jxcCategoryMapper.selectBySid(sid);
    }

    @Override
    public void deleteJxcCategorys(List<String> list, SysUser opUser) throws Exception {
        List<JxcCategory> categoryList = jxcCategoryMapper.selectBySids(list);
        if (categoryList.isEmpty()) {
            return;
        }
        List<String> inspectList = ListUtil.map(categoryList, JxcCategory::getSid);
        if (categoryList != null && list != null && inspectList.size() > 0 && inspectList.size() != list.size()) {
            List<String> filterNoExitSids = ListUtil.filter(list, t -> !inspectList.contains(t));
            if (filterNoExitSids != null && !filterNoExitSids.isEmpty()) {
                StringBuilder stringBuffer = new StringBuilder();
                stringBuffer.append("该").append(filterNoExitSids.size()).append("个记录不存在，例如：");
                for (String filterNoExitSid : filterNoExitSids) {
                    stringBuffer.append(filterNoExitSid + "、");
                }
                throw new HlpException(stringBuffer.toString());
            }
        }
        List<String> fullCodeList = ListUtil.map(categoryList, JxcCategory::getFull_code);
        List<JxcCategoryExt> allList = jxcCategoryMapper.selectListByFullCodeList(fullCodeList);
        if (allList == null || allList.isEmpty()) {
            return;
        }
        for (JxcCategory jxcCategory : allList) {
            autoInjectBaseData(jxcCategory, opUser, TYPE_DELETE);
        }
        jxcCategoryMapper.updateList(allList);
//        for (String sid : list) {
//            JxcCategory old = jxcCategoryMapper.selectBySid(sid);
//            super.checkEmpty(old, "该记录不存在");
//            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
//            jxcCategoryMapper.updateByPrimaryKeySelective(old);
//        }
    }

    @Override
    public void saveJxcCategory(JxcCategory record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            if (HlpUtils.isEmpty(record.getParent_sid())) {
                record.setParent_sid(ROOT_PARENT_SID);
            }
            validateRepeatJxcCategory(record);
            jxcCategoryMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            validateRepeatJxcCategory(record);
            jxcCategoryMapper.updateByPrimaryKeySelective(record);
        }
        this.setFullCode(record);
        if (!HlpUtils.isEmpty(record.getFull_code())) {
            jxcCategoryMapper.updateByPrimaryKeySelective(record);
        }
    }

    /**
     * 判断是否重复
     *
     * @param record
     */
    private void validateRepeatJxcCategory(JxcCategory record) throws HlpException {
        List<JxcCategoryExt> list = jxcCategoryMapper.validateRepeatJxcCategory(record);
        if (list.size() > 0) {
            List<JxcCategoryExt> codeRepeatList = ListUtil.filter(list, t -> t.getCode().equals(record.getCode()));
            if (codeRepeatList.size() > 0) {
                throw new HlpException("编号重复！");
            } else {
                throw new HlpException("名称重复！");
            }
        }
    }

    @Override
    public List<JxcCategoryExt> findJxcCategorys(Map<String, Object> params, PageBean page) throws Exception {
        return jxcCategoryMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcCategoryExt> findJxcCategorys(Map<String, Object> params) throws Exception {
        return jxcCategoryMapper.selectByParams(params);
    }

    @Override
    public List<JxcCategoryExt> findJxcCategoryTree(Map<String, Object> params) throws Exception {
        String parentSid = HlpUtils.getString(params, "parent_sid");
        if (!HlpUtils.isEmpty(parentSid)) {
            JxcCategoryExt assetsRegion = jxcCategoryMapper.selectBySid(parentSid);
            params.put("full_code", assetsRegion.getFull_code() + "%");
        }
        params.remove("parent_sid");
        List<JxcCategoryExt> list = jxcCategoryMapper.selectByParams(params);
        return TreeUtil.expandCategorysTree(list);
    }

    /**
     * 设置资源完整编码
     *
     * @param record
     * @throws HlpException
     */
    private void setFullCode(JxcCategory record) {
        if (HlpUtils.isEmpty(record.getParent_sid())) {
            record.setFull_code(record.getId() + ".");
        } else {
            JxcCategoryExt parent = jxcCategoryMapper.selectBySid(record.getParent_sid());
            if (parent != null) {
                record.setFull_code(parent.getFull_code() + record.getId() + ".");
            } else {
                record.setFull_code(record.getId() + ".");
            }
        }
    }

}
